﻿using System;
using System.Collections.Generic;
using System.Text;

namespace hedefgrup.reports.common
{
    public abstract class Filter
    {
        public ReportContext Context { get; set; }

        public virtual Report_Abstract Report
        {
            get
            {
                return Context.Report;
            }
        }

        public virtual string Name { get; set; }
        public abstract string Expression { get; }
        public List<string> Values = new List<string>();
        public abstract string Operator { get; }
        public abstract Type ValueType { get; }

        public virtual string[] GetValueList()
        {
            return null;
        }

        public virtual object ToActualValue()
        {
            return ToActualValues()[0];
        }

        public virtual object[] ToActualValues()
        {
            List<object> actualValues = new List<object>();

            if (ValueType != null)
            {
                try
                {
                    foreach (string value in Values)
                    {
                        actualValues.Add(Convert.ChangeType(value, ValueType));
                    }
                    return actualValues.ToArray();
                }
                catch
                {
                    return new object[] { null };
                }
            }
            else
            {
                return new object[] { Values[0] };
            }
        }

        public virtual object ToSqlStatementValue()
        {
            return ToSqlStatementValues()[0];
        }

        public virtual object[] ToSqlStatementValues()
        {
            return ToActualValues();
        }

        public virtual string ToSqlStatementFilter(ref int parameterOffset)
        {
            if (Values.Count > 1)
            {
                string statementFilter = "(";

                for (int i = 0; i < Values.Count; i++)
                {
                    if (this.ValueType == typeof(string) && Values[i] != null && Values[i].Contains("%"))
                    {
                        statementFilter += this.Expression + " LIKE ";
                    }
                    else
                    {
                        statementFilter += this.Expression + " " + this.Operator + " ";
                    }

                    statementFilter += "@p_" + parameterOffset++ + (i == Values.Count - 1 ? "" : " OR ");
                }
                
                statementFilter += ")";

                return statementFilter;
            }
            else
            {
                string sqlStatementValue = ToSqlStatementValue() as string;

                if (this.ValueType == typeof(string) && sqlStatementValue != null && sqlStatementValue.Contains("%"))
                {
                    return this.Expression + " LIKE " + "@p_" + parameterOffset++;
                }
                else
                {
                    return this.Expression + " " + this.Operator + " " + "@p_" + parameterOffset++;
                }
            }
        }
    }
}
